package com.safe.ele.util;
import com.safe.ele.config.RedisKeyConfig;
import com.safe.ele.config.SystemConfig;
import com.safe.ele.dto.ProdectDto;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

import java.util.concurrent.TimeUnit;

/**
 * @program: StartCoding
 * @description:
 * @author: Feri(邢朋辉)
 *
 *发送短信的而工具类：Redis缓存---短信有效期比较短  所以放在Redis比较合适
 * @create: 2021-01-06 14:48
 */
public class RedissonUtil {


    //实例化
    public static RedissonClient client;
    static {
        Config config=new Config();
        config.useSingleServer().
                //这里是Redis的缓存地址
                setAddress("redis://"+ SystemConfig.REDIS_HOST+":"+SystemConfig.REDIS_PORT).
                setPassword(SystemConfig.REDIS_PASS);
//        config.useSingleServer();//单机
//        config.useSentinelServers();//哨兵模式
//        config.useClusterServers();//集群模式
        client=Redisson.create(config);
    }
    //新增
    // String
    public static void setStr(String key,Object val,int seconds){
        client.getBucket(key).set(val,seconds, TimeUnit.SECONDS);
    }
    // hashmap
    public static void setHash(String key,String field,String val){
        client.getMap(key).put(field,val);
    }
    // list
    public static void setList(String key ,Object value){
        client.getList(key).add(value);
    }
    //set
    public static void setSet(String key,Object value){
        client.getSet(key).add(value);
    }

    //删除
    public static void delKeys(String... keys){
        client.getKeys().delete(keys);
    }
    public static void delField(String key,String field){
        client.getMap(key).remove(field);
    }
    //查询
    public static String getStr(String key){
        return client.getBucket(key).get().toString();
    }
    public static String getHash(String key,String field){
        return client.getMap(key).get(field).toString();
    }
    public static boolean checkField(String key,String field){
        return client.getMap(key).containsKey(field);
    }
    public static boolean isHave(String key){
        return client.getKeys().countExists(key)>0;
    }
    public static Object getList(String key){
        return client.getList(key);
    }
    //set
    public static Object getSet(String key){
        return client.getSet(key);
    }
}
